#ifndef SHOWIMAGEWIDGET_H
#define SHOWIMAGEWIDGET_H

#include <QWidget>
#include <QWidgetList>
#include <QStackedWidget>
#include <QLabel>
#include <QPainter>
#include <QImage>
#include <QMenu>
#include <QAction>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QImage>
#include <QFile>
#include <QPdfWriter>
#include <QResizeEvent>
#include <QKeyEvent>
#include <QStack>
#include <QMatrix>
#include <QThread>

#include <opencv4/opencv2/core.hpp>
#include <opencv4/opencv2/imgcodecs.hpp>
#include <opencv4/opencv2/imgproc.hpp>
using namespace cv;

//#include <kylin_image_codec/kylinimagecodec.h>

#include "globalsignal.h"
#include "saneobject.h"
#include "toolbarwidget.h"
#include "watermarkdialog.h"
#include "crop.h"
#include "beauty.h"
#include "rectify.h"
#include "include/common.h"
#include "../codec/kylinimagecodec/kylinimagecodec.h"

#define ShowImageWidgetMinimumSize QSize(387, 536)



class ShowImageWidget : public QWidget
{
    Q_OBJECT
public:
    explicit ShowImageWidget(QWidget *parent = nullptr);


    void setupGui();
    void initConnect();

    int toUnicode(QString str);
    void setPdfSize(QPdfWriter *pdfWriter, QString size);
    void saveToPdf(QImage img, QString pathname);

    QImage *imageSave(QString filename);

    QString setPixmapScaled(QImage img, QLabel *lab, double scale = 1.0);

    void setPixmapScaledByProportion(double scaledNumber);

    enum ZoomStatus {
        ZoomOut,
        ZoomIn
    };

    void getCropArea(int &x1, int &y1, int &x2, int &y2);

    double getCurrentPercentage();
    double getRealPercentage(ZoomStatus zoomType);
    void updatePercentageByZoom(double scale);

    const QString scannerImagePath;
    const int defaultImageLableWidth = 387;
    const int defaultImageLableHeight = 536;

    QSize defaultScanImageSize;


protected:
    void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
    void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
    bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;


public slots:
    void showNormalImageAfterScan();
    QPixmap resizePix(const QPixmap &pixmap , const QSize &size);

    void showImageAfterClickedThumbnail(QString loadPath);

    void saveImage(QString filename);

    void cropSlot();
    void rotateSlot();
    void mirrorSlot();
    void watermarkSlot();
    void zoomoutNormalImage();
    void zoominNormalImage();
    void zoomCurrentNormalImage();

    void beautyStartSlot();
    void beautyStopSlot();
    void rectifyStartSlot();
    void rectifyStopSlot(bool isTrue);
    void ocrStartSlot();
    void ocrStopSlot();

    void saveCurrentPicture();
    void loadAfterBROPicture();

private:
    QStack<QImage> m_imageStack;
    bool rotationFlag = false;
    double proportion = 1.0;
    double proportionForPercentage = 1.0;

    Mat matImage;

    QImage *m_stackImage;
    QImage *m_editImage;
    QImage *m_normalImage;
    QPixmap m_normalPixmap;

    QLabel *m_showImageLabel;
    CropLabel *m_cropLabel;
    QStackedWidget *m_showImageAndCropWidget;
    QHBoxLayout *m_showImageHLayout;

    ToolBarWidget *m_toolbarWidget;

    QVBoxLayout *m_mainVLayout;

};

#endif // SHOWIMAGEWIDGET_H
